package algocasts;

import java.util.LinkedList;
import java.util.Queue;

public class P47_invertBinaryTree {
    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
    }

    public TreeNode invertBinaryTreeRecursive(TreeNode root) {
        if (root == null) return root;
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;
        invertBinaryTreeRecursive(root.left);
        invertBinaryTreeRecursive(root.right);
        return root;
    }

    public TreeNode invertBinaryTreeNodeIterative(TreeNode root) {
        if (root == null) return root;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            TreeNode tmp = node.left;
            node.left = node.right;
            node.right = tmp;
            if (node.left!=null) queue.add(node.left);
            if (node.right!=null) queue.add(node.right);
        }
        return root;
    }


}
